package com.dapeng.cleardatabackend.config;

import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author dapeng
 * @description redisson配置
 * @date 2024/12/7 15:56
 */
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedissonConfig {

    /**
     * 主机
     */
    private String host;

    /**
     * 端口
     */
    private String port;

    /**
     * 密码
     */
    private String password;

    /**
     * redisson数据库
     */
    private Integer redissonDatabase;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        String redisUrl = String.format("redis://%s:%s", host, port);
        config.setLockWatchdogTimeout(10000L)
                .setCodec(new org.redisson.codec.JsonJacksonCodec())
                .useSingleServer().setAddress(redisUrl)
                .setPassword(password)
                .setDatabase(redissonDatabase);
        return Redisson.create(config);
    }
}
